const ipUtil = require("../../utils/hostUtil")
const requestUtil = require("../../utils/requestUtil")
Page({

    /**
     * 页面的初始数据
     */
    data: {
        // 当前id
        id: "",
        // 后端返回数据
        address: "",
        // 通过地图选择的地址
        chooseAddress: null,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this;
        wx.setNavigationBarTitle({title: '收货地址'})
        // 接收从编辑传入的id 如果有id 则为编辑 反之则为新增
        var id = options.id || "";
        this.setData({
            id: id
        })
        if (id != "") {
            wx.request({
                url: `${ipUtil.host}${requestUtil.address_detail}`,
                data: {addressId: id},
                header: requestUtil.header,
                method: "POST",
                success(res) {
                    if (res) {
                        that.setData({
                            address: res.data
                        })
                    }
                }
            })
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    /**
     * 通过地图选择地址
     */
    openLocation: function () {
        var that = this;
        wx.chooseLocation({
            success: function (res) {
                that.setData({
                    chooseAddress: res.name
                });
            }, fail(res) {
                // 引导用户重新授权
                wx.showModal({
                    title: "请授权我们使用地址",
                    success(res) {
                        if (res.confirm) {
                            wx.openSetting({
                                success(res) {
                                    wx.chooseLocation({
                                        success: function (res) {
                                            that.setData({
                                                chooseAddress: res.name
                                            });
                                        }
                                    })
                                }
                            })
                        }
                    }
                })
            }
        });
    },
    /**
     * 提交更改
     * @param e
     */
    forsubmit: function (e) {
        var detail = e.detail.value;
        if (detail.telNo == "" || detail.telNo.length != 11) {
            wx.showToast({
                title: "请输入正确的手机号",
                icon:"none",
                duration: 2000
            })
            return
        }
        if(detail.username==""){
            wx.showToast({
                title: "请输入收货人",
                icon:"none",
                duration: 2000
            })
            return
        }
        if (detail.address == "") {
            wx.showToast({
                title: "请选择收货地址",
                icon:"none",
                duration: 2000
            })
            return
        }
        if (detail.detailedAdd == "") {
            wx.showToast({
                title: "请输入详细收货地址",
                icon:"none",
                duration: 2000
            })
            return
        }
        wx.request({
            url: `${ipUtil.host}${requestUtil.address_save}?token=${wx.getStorageSync("token")}`,
            data: {
                addressid: detail.id,
                username: detail.username,
                telNo: detail.telNo,
                address: detail.address,
                detailedAdd: detail.detailedAdd,
                defaultAdd: detail.defaultAdd ? 1 : 0,
            },
            method: "POST",
            success(res) {
                wx.showToast({
                    title: "提交成功",
                    icon: "none",
                    duration: 1500
                })
            }
        })
    }, deleteAddress: function (e) {
        var id = e.currentTarget.dataset.id;
        wx.request({
            url: `${ipUtil.host}${requestUtil.address_delete}`,
            data: {addressId: id},
            method: "POST",
            header: requestUtil.header,
            success(res) {
                wx.showToast({
                    title: "操作成功",
                    icon: "none",
                    duration: 1000
                })
                wx.navigateBack({delta: 1});
            }
        })
    },
    /**
     * 新增收货地址
     * @param e
     */
    forinsert: function (e) {
        var detail = e.detail.value;
        if (detail.telNo == "" || detail.telNo.length != 11) {
            wx.showToast({
                title: "请输入正确的手机号",
            })
            return
        }
        wx.request({
            url: `${ipUtil.host}${requestUtil.address_add}?token=${wx.getStorageSync("token")}`,
            data: {
                addressid: "",
                username: detail.username,
                telNo: detail.telNo,
                address: detail.address,
                detailedAdd: detail.detailedAdd,
                defaultAdd: detail.defaultAdd ? 1 : 0
            },
            method: "POST",
            success(res) {
                wx.showToast({
                    title: "提交成功",
                    icon: "none",
                    duration: 1500
                })
                // 返回上一层
                wx.navigateBack({delta: 1});
            }
        })
    }
})